package org.example.mq.mqserver.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.mq.common.ConsumerEnv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class MSGQueue {
    //唯一标识
    private String name;
    //是否持久化保存
    private boolean durable = false;
    //是否独占
    private boolean exclusive = false;
    //是否自动删除，如果没有交换机使用，会自动删除这个队列
    private boolean autoDelete = false;
    //扩展属性(序列化为json字符串，然后写入数据库)
    private Map<String, Object> arguments = new HashMap<>();
    //使用list记录包含的消费者
    private List<ConsumerEnv> consumerEnvs = new ArrayList<>();
    //记录到哪个消费者了(线程安全)
    private AtomicInteger consumerSeq = new AtomicInteger();

    /*
    * 一共增加消费者和删除消费者的操作
    * */
    public void addConsumerEnv(ConsumerEnv consumerEnv){
        synchronized (this){
            consumerEnvs.add(consumerEnv);
        }
    }

    //使用轮训的方式，挑选消费者，处理当前消息
    public ConsumerEnv chooseConsumer(){
        if(consumerEnvs.size()==0){
            return null;
        }
        int index = consumerSeq.get() % consumerEnvs.size();
        consumerSeq.getAndIncrement();
        return consumerEnvs.get(index);
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

/*    public Map<String, Object> getArguments() {
        return arguments;
    }

    public void setArguments(Map<String, Object> arguments) {
        this.arguments = arguments;
    }*/

    /*
    * 获取其他属性（json变成字符串）
    * */
    public String getArguments(){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }

    /*
    * 设置其他属性（字符串变成json）
    * */
    public void setArguments(String argumentsJson){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String,Object>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public Object getArguments(String key){
        return arguments.get(key);
    }

    public void setArguments(String key ,Object value){
        arguments.put(key, value);
    }

    public void setArguments(Map<String, Object> arguments) {
        this.arguments = arguments;
    }
}
